/*You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "车辆.h"

#include <vehicle/PxVehicleWheels.h>
#include <vehicle2/PxVehicleParams.h>

#include "snippetvehicle2common/enginedrivetrain/EngineDrivetrain.h"
#include "snippetvehicle2common/serialization/BaseSerialization.h"
#include "snippetvehicle2common/serialization/EngineDrivetrainSerialization.h"
#include "snippetvehicle2common/SnippetVehicleHelpers.h"


using namespace physx;
using namespace physx::vehicle2;
using namespace snippetvehicle2;

static PxVehiclePhysXMaterialFriction gPhysXMaterialFrictions[16];
static PxU32 gNbPhysXMaterialFrictions = 1;
static PxReal gPhysXDefaultMaterialFriction = 1.0f;

static std::map<physx::PxScene*, std::vector<S_PhyVehicle*>> g场景模拟车辆;
static std::map<physx::PxScene*, PxVehiclePhysXSimulationContext> g场景车辆模拟环境;

static auto gVehicleDataPath = "D:/DEV/PhysX/physx/snippets/media/vehicledata/";




PxShape* f_创建车轮形状(float32 半径, float32 宽度) {
	PxVec3 verts[32];
	for (PxU32 k = 0; k < 16; k++) {
		const PxF32 lng = 半径 * PxCos(k*2.0f*PxPi / 16.0f);
		const PxF32 lat = 宽度;
		const PxF32 vrt = 半径 * PxSin(k*2.0f*PxPi / 16.0f);

		verts[2 * k + 0] = PxVec3(lng, lat, vrt);
		verts[2 * k + 1] = PxVec3(lng, -lat, vrt);
	}

	// Create descriptor for convex mesh
	PxConvexMeshDesc convexDesc;
	convexDesc.points.count = 32;
	convexDesc.points.stride = sizeof(PxVec3);
	convexDesc.points.data = verts;
	convexDesc.flags = PxConvexFlag::eCOMPUTE_CONVEX;

	PxConvexMesh* convexMesh = NULL;
	PxDefaultMemoryOutputStream buf;
	if (PxCookConvexMesh(S_Px物理引擎管理::f_getParams(), convexDesc, buf)) {
		PxDefaultMemoryInputData id(buf.getData(), buf.getSize());
		convexMesh = S_Px物理引擎管理::gPhysics->createConvexMesh(id);
	}

	PxConvexMeshGeometry convexMeshGeom(convexMesh);
	PxShape* wheelShape = S_Px物理引擎管理::gPhysics->createShape(convexMeshGeom, *S_Px物理引擎管理::gPxMaterial, true);
	wheelShape->setFlags(physx::PxShapeFlag::eSIMULATION_SHAPE);
	//wheelShape->setSimulationFilterData(wheelShapeParams.simulationFilterData);
	//wheelShape->setQueryFilterData(wheelShapeParams.queryFilterData);
	return wheelShape;
}




class S_PxVehicle : public S_PhyVehicle
	//, public PhysXActorVehicle
{
	
public:
	EngineDriveVehicle m_Vehicle;
	PxVehiclePhysXSimulationContext* m_VehicleSimulationContext;
	PxVehicleDrivableSurfaceToTireFrictionPairs* m_surfaceTirePairs;
	PxVehicleDrive4W* m_车辆实例;

	S_PxVehicle() {
		PxInitVehicleSDK(*S_Px物理引擎管理::gPhysics);

		m_车辆实例 = nullptr;

		m_VehicleSimulationContext = nullptr;

		gPhysXMaterialFrictions[0].friction = 1.0f;
		gPhysXMaterialFrictions[0].material = S_Px物理引擎管理::gPxMaterial;

		m_surfaceTirePairs = PxVehicleDrivableSurfaceToTireFrictionPairs::allocate(16, 16);
		for (PxU32 i = 0; i < 16; ++i) {
			for (PxU32 j = 0; j < 16; ++j) {
				m_surfaceTirePairs->setTypePairFriction(i, j, 0.8); // 设置摩擦系数
			}
		}

		//mComponentSequence.add(static_cast<PxVehiclePhysXActorBeginComponent*>(this));
		//mComponentSequence.add(new PxVehicleSuspensionComponent());
		//mComponentSequence.add(new PxVehicleTireComponent());
		//mComponentSequence.add(new PxVehiclePhysXActorEndComponent());
		//
		//mComponentSequence.beginSubstepGroup(3); // 开始一个子步分组，更新3次
		//mComponentSequence.add(new PxVehicleSuspensionComponent());
		//mComponentSequence.add(new PxVehicleTireComponent());
		//mComponentSequence.endSubstepGroup(); // 结束子步分组
		//
		//mComponentSequence.add(static_cast<PxVehiclePhysXActorEndComponent*>(this));
		//return;

		m_Vehicle.mBaseParams.axleDescription;

		m_Vehicle.mBaseParams.frame.setToDefault();

		m_Vehicle.mBaseParams.scale;

		m_Vehicle.mBaseParams.rigidBodyParams.mass = 2014;
		m_Vehicle.mBaseParams.rigidBodyParams.moi = {3200,3414,750};

		m_Vehicle.mBaseParams.suspensionStateCalculationParams.limitSuspensionExpansionVelocity = false;

		//油门刹车
		m_Vehicle.mBaseParams.brakeResponseParams[0].maxResponse = 1875.0;
		//手刹
		m_Vehicle.mBaseParams.brakeResponseParams[1].maxResponse = 1875.0;

		//转向参数
		m_Vehicle.mBaseParams.steerResponseParams;

		m_Vehicle.mBaseParams.ackermannParams;

		m_Vehicle.mBaseParams.suspensionParams;
		m_Vehicle.mBaseState.setToDefault();


		m_Vehicle.mEngineDriveParams.gearBoxParams.neutralGear = 1;
		m_Vehicle.mEngineDriveParams.gearBoxParams.finalRatio = 4;
		m_Vehicle.mEngineDriveParams.gearBoxParams.nbRatios = 7;
		m_Vehicle.mEngineDriveParams.gearBoxParams.switchTime = 0.5;
		m_Vehicle.mEngineDriveParams.gearBoxParams.ratios[0] = -4.0;
		m_Vehicle.mEngineDriveParams.gearBoxParams.ratios[1] = 0;
		m_Vehicle.mEngineDriveParams.gearBoxParams.ratios[2] = 4.0;
		m_Vehicle.mEngineDriveParams.gearBoxParams.ratios[3] = 2;
		m_Vehicle.mEngineDriveParams.gearBoxParams.ratios[4] = 1.5;
		m_Vehicle.mEngineDriveParams.gearBoxParams.ratios[5] = 1.1;
		m_Vehicle.mEngineDriveParams.gearBoxParams.ratios[6] = 1.0;

		m_Vehicle.mEngineDriveParams.autoboxParams.upRatios[0] = 0.65;
		m_Vehicle.mEngineDriveParams.autoboxParams.upRatios[1] = 0.15;
		m_Vehicle.mEngineDriveParams.autoboxParams.upRatios[2] = 0.65;
		m_Vehicle.mEngineDriveParams.autoboxParams.upRatios[3] = 0.65;
		m_Vehicle.mEngineDriveParams.autoboxParams.upRatios[4] = 0.65;
		m_Vehicle.mEngineDriveParams.autoboxParams.upRatios[5] = 0.65;
		m_Vehicle.mEngineDriveParams.autoboxParams.upRatios[6] = 0.65;
		m_Vehicle.mEngineDriveParams.autoboxParams.downRatios[0] = 0.5;
		m_Vehicle.mEngineDriveParams.autoboxParams.downRatios[1] = 0.5;
		m_Vehicle.mEngineDriveParams.autoboxParams.downRatios[2] = 0.5;
		m_Vehicle.mEngineDriveParams.autoboxParams.downRatios[3] = 0.5;
		m_Vehicle.mEngineDriveParams.autoboxParams.downRatios[4] = 0.5;
		m_Vehicle.mEngineDriveParams.autoboxParams.downRatios[5] = 0.5;
		m_Vehicle.mEngineDriveParams.autoboxParams.downRatios[6] = 0.5;
		m_Vehicle.mEngineDriveParams.autoboxParams.latency = 2;

		m_Vehicle.mEngineDriveParams.clutchCommandResponseParams.maxResponse = 10.0;
		m_Vehicle.mEngineDriveParams.engineParams.torqueCurve.addPair(0,1);
		m_Vehicle.mEngineDriveParams.engineParams.torqueCurve.addPair(0.33,1);
		m_Vehicle.mEngineDriveParams.engineParams.torqueCurve.addPair(1,1);
		m_Vehicle.mEngineDriveParams.engineParams.peakTorque = 500;
		m_Vehicle.mEngineDriveParams.engineParams.idleOmega = 0;
		m_Vehicle.mEngineDriveParams.engineParams.maxOmega = 600;
		m_Vehicle.mEngineDriveParams.engineParams.dampingRateFullThrottle = 0.15;
		m_Vehicle.mEngineDriveParams.engineParams.dampingRateZeroThrottleClutchEngaged = 2.0;
		m_Vehicle.mEngineDriveParams.engineParams.dampingRateZeroThrottleClutchDisengaged = 0.35;

		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.setToDefault();
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.torqueRatios[0] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.torqueRatios[1] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.torqueRatios[2] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.torqueRatios[3] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.torqueRatios[4] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.torqueRatios[5] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.aveWheelSpeedRatios[0] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.aveWheelSpeedRatios[1] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.aveWheelSpeedRatios[2] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.aveWheelSpeedRatios[3] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.aveWheelSpeedRatios[4] = 0.25;
		m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.aveWheelSpeedRatios[5] = 0.25;

		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.setToDefault();
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.torqueRatios[0] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.torqueRatios[1] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.torqueRatios[2] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.torqueRatios[3] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.torqueRatios[4] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.torqueRatios[5] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.aveWheelSpeedRatios[0] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.aveWheelSpeedRatios[1] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.aveWheelSpeedRatios[2] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.aveWheelSpeedRatios[3] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.aveWheelSpeedRatios[4] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.aveWheelSpeedRatios[5] = 0.25;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.frontWheelIds[0] = 0;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.frontWheelIds[1] = 1;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.rearWheelIds[0] = 2;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.rearWheelIds[1] = 3;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.centerBias = 1.3;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.centerTarget = 1.29;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.frontBias = 1.3;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.frontTarget = 1.29;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.rearBias = 1.3;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.rearTarget = 1.29;
		m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.rate = 10.0;

		m_Vehicle.mEngineDriveParams.tankDifferentialParams.setToDefault();
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.torqueRatios[0] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.torqueRatios[1] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.torqueRatios[2] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.torqueRatios[3] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.torqueRatios[4] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.torqueRatios[5] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.aveWheelSpeedRatios[0] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.aveWheelSpeedRatios[1] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.aveWheelSpeedRatios[2] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.aveWheelSpeedRatios[3] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.aveWheelSpeedRatios[4] = 0.25;
		m_Vehicle.mEngineDriveParams.tankDifferentialParams.aveWheelSpeedRatios[5] = 0.25;
		//m_Vehicle.mEngineDriveParams.tankDifferentialParams.addTankTrack(2, { 0,2 }, 0);

		m_Vehicle.mEngineDriveParams.clutchParams.accuracyMode = physx::vehicle2::PxVehicleClutchAccuracyMode::Enum::eBEST_POSSIBLE;
		m_Vehicle.mEngineDriveParams.clutchParams.estimateIterations = 5;

		m_Vehicle.mCommandState.setToDefault();
		//m_VehicleSimulationContext.setToDefault();
		//m_VehicleSimulationContext.frame.lngAxis = PxVehicleAxes::ePosZ;
		//m_VehicleSimulationContext.frame.latAxis = PxVehicleAxes::ePosX;
		//m_VehicleSimulationContext.frame.vrtAxis = PxVehicleAxes::ePosY;
		//m_VehicleSimulationContext.scale.scale = 1.0f;
		//m_VehicleSimulationContext.gravity = gGravity;
		//m_VehicleSimulationContext.physxScene = gScene;
		//m_VehicleSimulationContext.physxActorUpdateMode = PxVehiclePhysXActorUpdateMode::eAPPLY_ACCELERATION;
	}
	~S_PxVehicle() {

	}

	bool f_Init(S_PhyVehicleParams& 车身) {
		//m_Vehicle.mBaseParams.scale.setToDefault();
		//readBaseParamsFromJsonFile(gVehicleDataPath, "Base_n1.json", m_Vehicle.mBaseParams);

		m_Vehicle.mBaseParams.frame.setToDefault();
		m_Vehicle.mBaseParams.rigidBodyParams.moi.x = 车身.m_惯性矩.x;
		m_Vehicle.mBaseParams.rigidBodyParams.moi.y = 车身.m_惯性矩.y;
		m_Vehicle.mBaseParams.rigidBodyParams.moi.z = 车身.m_惯性矩.z;
		m_Vehicle.mBaseParams.rigidBodyParams.mass = 车身.m_车重;
		//m_Vehicle.mBaseParams.frame.lngAxis = PxVehicleAxes::ePosY;
		//m_Vehicle.mBaseParams.frame.latAxis = PxVehicleAxes::ePosX;
		//m_Vehicle.mBaseParams.frame.vrtAxis = PxVehicleAxes::ePosZ;

		setPhysXIntegrationParams(m_Vehicle.mBaseParams.axleDescription,
								  gPhysXMaterialFrictions, gNbPhysXMaterialFrictions, gPhysXDefaultMaterialFriction,
								  m_Vehicle.mPhysXParams);
		readEngineDrivetrainParamsFromJsonFile(gVehicleDataPath, "EngineDrive.json", m_Vehicle.mEngineDriveParams);

		m_Vehicle.mPhysXParams.m_车身几何体 = 车身.m_geom;
		//m_Vehicle.mPhysXParams.physxActorBoxShapeLocalPose.p = { 车身.m_local.x, 车身.m_local.y, 车身.m_local.z };
		//m_Vehicle.mPhysXParams.physxActorBoxShapeLocalPose.q = { 车身.m_pose.x, 车身.m_pose.y, 车身.m_pose.z, 车身.m_pose.w };

		for (uint32 i = 0; i < 4; ++i) {
			m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.torqueRatios[i] = 0.25;
			//m_Vehicle.mEngineDriveParams.multiWheelDifferentialParams.aveWheelSpeedRatios[i] = 0.25;
		}
		//m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.centerBias = 0.5;
		//m_Vehicle.mEngineDriveParams.fourWheelDifferentialParams.centerTarget = 0.5;
		return true;
	}

	void f_update(float32 dt) {

		//PxVehicleUpdates(dt, {0,0,-8}, *m_surfaceTirePairs, 4, )
		//PxVehicleDrive4WUpdate()
		m_Vehicle.mComponentSequence.setSubsteps(m_Vehicle.mComponentSequenceSubstepGroupHandle, 1);
		m_Vehicle.step(dt, *m_VehicleSimulationContext);
	}
};

S_PhyVehicle* f_px_创建车辆(S_PhyVehicleParams& 车身
						, S_PhyWheel** 车轮, uint32 车轮数量
						, const S_PxAxleParams& 车轴属性) {

	PxTransform pose;
	pose.p = { 车身.m_local.x, 车身.m_local.y, 车身.m_local.z };
	pose.q = { 车身.m_pose.x, 车身.m_pose.y, 车身.m_pose.z, 车身.m_pose.w };
	S_PxVehicle* vehicle = new S_PxVehicle;

	/*float32 转向校正 = 0;
	//std::vector<PxShape*> 车轮形状;
	
	vehicle->m_车辆实例 = PxVehicleDrive4W::allocate(车轮数量);
	//vehicle->m_车辆实例 = new PxVehicleDrive;
	

	PxVehicleWheelsSimData* 车轮模拟数据 = PxVehicleWheelsSimData::allocate(车轮数量);
	for (PxU32 i = 0; i < 车轮数量; ++i) {
		PxVehicleWheelData 车轮数据;
		车轮数据.mRadius = 车轮[i]->m_params->radius;
		车轮数据.mWidth = 车轮[i]->m_params->halfWidth;
		车轮数据.mMOI = 车轮[i]->m_params->moi;
		车轮数据.mDampingRate = 车轮[i]->m_params->dampingRate;
		车轮模拟数据->setWheelData(i, 车轮数据);
		
		//mFrontWidth
		physx::PxFilterData qryFilterData;
		qryFilterData.setToDefault();

		PxVehicleTireData 轮胎数据;
		轮胎数据.mLatStiffX = 车轮[i]->m_tireForceParams->latStiffX;
		轮胎数据.mLatStiffY = 车轮[i]->m_tireForceParams->latStiffY;
		轮胎数据.mFrictionVsSlipGraph[0][0] = 车轮[i]->m_tireForceParams->frictionVsSlip[0][0];
		轮胎数据.mFrictionVsSlipGraph[0][1] = 车轮[i]->m_tireForceParams->frictionVsSlip[0][1];
		轮胎数据.mFrictionVsSlipGraph[1][0] = 车轮[i]->m_tireForceParams->frictionVsSlip[1][0];
		轮胎数据.mFrictionVsSlipGraph[1][1] = 车轮[i]->m_tireForceParams->frictionVsSlip[1][1];
		轮胎数据.mFrictionVsSlipGraph[2][0] = 车轮[i]->m_tireForceParams->frictionVsSlip[2][0];
		轮胎数据.mFrictionVsSlipGraph[2][1] = 车轮[i]->m_tireForceParams->frictionVsSlip[2][1];

		PxVehicleSuspensionData 悬挂数据;
		悬挂数据.mMaxCompression = 车轮[i]->m_suspensionParams->suspensionTravelDist;
		悬挂数据.mMaxDroop = 车轮[i]->m_suspensionParams->suspensionTravelDist * 2;
		悬挂数据.mSpringStrength = 车轮[i]->m_suspensionForceParams->stiffness;
		悬挂数据.mSpringDamperRate  = 车轮[i]->m_suspensionForceParams->damping;
		悬挂数据.mSprungMass   = 车轮[i]->m_suspensionForceParams->sprungMass;
		车轮模拟数据->setSuspensionData(i, 悬挂数据);
		车轮模拟数据->setSuspTravelDirection(i, 车轮[i]->m_suspensionParams->suspensionTravelDir);
		车轮模拟数据->setWheelCentreOffset(i, 车轮[i]->m_suspensionParams->suspensionAttachment.p);
		车轮模拟数据->setSuspForceAppPointOffset(i, 车轮[i]->m_suspensionParams->suspensionAttachment.p + PxVec3(0,0,0.1));
		车轮模拟数据->setTireForceAppPointOffset(i, 车轮[i]->m_suspensionParams->suspensionAttachment.p - PxVec3(0,0,1));
		车轮模拟数据->setSceneQueryFilterData(i, qryFilterData);
		车轮模拟数据->setTireData(i, 轮胎数据);
		
		auto 车轮形状 = f_创建车轮形状(车轮数据.mRadius, 车轮数据.mWidth);
		车身.m_车身刚体->attachShape(*车轮形状);
		车轮形状->release();
	}


	PxVehicleDriveSimData4W VehicleDriveSimData4W;
	PxVehicleAckermannGeometryData VehicleAckermannGeometryData;
	VehicleAckermannGeometryData.mAccuracy = 1;
	VehicleAckermannGeometryData.mFrontWidth = 车轴属性.m_两轮之间距离[0];
	VehicleAckermannGeometryData.mRearWidth = 车轴属性.m_两轮之间距离[1];
	VehicleAckermannGeometryData.mAxleSeparation = 车轴属性.m_两轴距离[0];
	vehicle->m_车辆实例->mDriveSimData.setAckermannGeometryData(VehicleAckermannGeometryData);

	// 设置车辆传动系统
	PxVehicleClutchData 离合器数据;
	离合器数据.mStrength = 10.0;
	vehicle->m_车辆实例->mDriveSimData.setClutchData(离合器数据);

	PxVehicleDifferential4WData 传动数据;
	传动数据.mType = PxVehicleDifferential4WData::eDIFF_TYPE_LS_4WD;
	传动数据.mRearLeftRightSplit = 0.5;
	传动数据.mFrontRearSplit = 0.0f;

	vehicle->m_车辆实例->mDriveDynData.setAnalogInput(PxVehicleDrive4WControl::eANALOG_INPUT_ACCEL, 0.5f);
	vehicle->m_车辆实例->mDriveDynData.setAnalogInput(PxVehicleDrive4WControl::eANALOG_INPUT_BRAKE, 0.0f);
	vehicle->m_车辆实例->mDriveDynData.setAnalogInput(PxVehicleDrive4WControl::eANALOG_INPUT_HANDBRAKE, 0.0f);
	vehicle->m_车辆实例->mDriveDynData.setAnalogInput(PxVehicleDrive4WControl::eANALOG_INPUT_STEER_LEFT, 0.0f);
	vehicle->m_车辆实例->mDriveDynData.setAnalogInput(PxVehicleDrive4WControl::eANALOG_INPUT_STEER_RIGHT, 0.0f);

	vehicle->m_车辆实例->mWheelsSimData.getSuspensionData(0);
	
	VehicleDriveSimData4W.setAckermannGeometryData(VehicleAckermannGeometryData);
	VehicleDriveSimData4W.setClutchData(离合器数据);
	VehicleDriveSimData4W.setDiffData(传动数据);
	
	auto bodytype = 车身.m_车身刚体->getType();
	if (bodytype == PxActorType::eRIGID_DYNAMIC) {
		vehicle->m_车辆实例->setup(S_Px物理引擎管理::gPhysics, static_cast<PxRigidDynamic*>(车身.m_车身刚体), *车轮模拟数据, VehicleDriveSimData4W, 0);
	}

	for (PxU32 i = 0; i < 车轮数量; ++i) {
		vehicle->m_车辆实例->mWheelsSimData.setWheelShapeMapping(i, i+1);
	}
	
	return vehicle;*/
	

	vehicle->m_Vehicle.mBaseParams.scale.setToDefault();
	readBaseParamsFromJsonFile(gVehicleDataPath, "Base_n1.json", vehicle->m_Vehicle.mBaseParams);

	vehicle->f_Init(车身);

	for (uint32 i = 0; i < 车轮数量; ++i) {
		vehicle->m_Vehicle.mBaseParams.wheelParams[i] = *车轮[i]->m_params;
		vehicle->m_Vehicle.mBaseParams.suspensionParams[i] = *车轮[i]->m_suspensionParams;
		//vehicle->m_Vehicle.mBaseParams.suspensionParams[i].suspensionAttachment = 车轮[i]->m_suspensionParams->suspensionAttachment;
		//vehicle->m_Vehicle.mBaseParams.suspensionParams[i].suspensionTravelDir = 车轮[i]->m_suspensionParams->suspensionTravelDir;
		vehicle->m_Vehicle.mBaseParams.suspensionForceParams[i] = *车轮[i]->m_suspensionForceParams;
		//vehicle->m_Vehicle.mBaseParams.tireForceParams[i] = *车轮[i]->m_tireForceParams;
		//vehicle->m_Vehicle.mBaseParams.suspensionComplianceParams[i].suspForceAppPoint.yVals[0] = PxVec3(0, 0, -0.2);
		//vehicle->m_Vehicle.mBaseParams.suspensionComplianceParams[i].tireForceAppPoint.yVals[0] = PxVec3(0, 0, -0.2);
		//vehicle->m_Vehicle.mBaseParams.suspensionStateCalculationParams.suspensionJounceCalculationType = PxVehicleSuspensionJounceCalculationType::Enum(1);
		//vehicle->m_Vehicle.mBaseParams.suspensionStateCalculationParams.limitSuspensionExpansionVelocity = false;
		//vehicle->m_Vehicle.mBaseParams.tireForceParams[i] = {0.009, 143909, 24525, 0, };
	}


	//Set the states to default.
	if (!vehicle->m_Vehicle.initialize(*S_Px物理引擎管理::gPhysics, S_Px物理引擎管理::f_getParams(), *S_Px物理引擎管理::gPxMaterial, EngineDriveVehicle::eDIFFTYPE_FOURWHEELDRIVE)) {
		return nullptr;
	}

	//设置挡位
	vehicle->m_Vehicle.mEngineDriveState.gearboxState.currentGear = vehicle->m_Vehicle.mEngineDriveParams.gearBoxParams.neutralGear + 1;
	vehicle->m_Vehicle.mEngineDriveState.gearboxState.targetGear = vehicle->m_Vehicle.mEngineDriveParams.gearBoxParams.neutralGear + 1;

	//设置自动变速箱
	vehicle->m_Vehicle.mTransmissionCommandState.targetGear = PxVehicleEngineDriveTransmissionCommandState::eAUTOMATIC_GEAR;
	vehicle->m_Vehicle.mTransmissionCommandState.clutch = 0;


	//车身.m_车身刚体->setCMassLocalPose(pose);
	车身.m_车身刚体->setGlobalPose(pose);
	//for (auto& e : 车轴属性.m_车轴包含车轮ID) {
	//	vehicle->m_Vehicle.mBaseParams.axleDescription.addAxle(e.size(), e.data());
	//}
	//vehicle->f_update(1 / 60.0f);
	return vehicle;

}

physx::PxActor* f_px_get_车身刚体(S_PhyVehicle* v) {
	S_PxVehicle* a = static_cast<S_PxVehicle*>(v);
	//return a->m_车辆实例->getRigidDynamicActor();
	return a->m_Vehicle.mPhysXState.physxActor.rigidBody;
}

void f_phy_添加模拟车辆(physx::PxScene* scene, S_PhyVehicle* 车辆) {
	g场景模拟车辆[scene].push_back(车辆);

	auto* vehicle = dynamic_cast<S_PxVehicle*>(车辆);
	vehicle->m_VehicleSimulationContext = &g场景车辆模拟环境[scene];
	//vehicle->m_VehicleSimulationContext->physxScene = scene;
	vehicle->m_Vehicle.mPhysXState.physxActor.rigidBody->setName("mBaseState");
}

void f_phy_重置车辆模拟(physx::PxScene* scene) {
	g场景模拟车辆[scene].clear();
	g场景车辆模拟环境[scene] = {};
}

void f_phy_重置车辆模拟环境(physx::PxScene* scene) {
	g场景车辆模拟环境[scene].setToDefault();
	g场景车辆模拟环境[scene].frame.setToDefault();
	g场景车辆模拟环境[scene].gravity = scene->getGravity();
	g场景车辆模拟环境[scene].physxScene = scene;
	g场景车辆模拟环境[scene].scale.scale = 1.0f;
	g场景车辆模拟环境[scene].physxActorUpdateMode = PxVehiclePhysXActorUpdateMode::eAPPLY_ACCELERATION;
}

void f_phy_车辆模拟(physx::PxScene* scene, float32 dt) {
	for (auto& e : g场景模拟车辆[scene]) {
		e->f_update(dt);
	}
}

void f_phy_车辆操控状态(S_PhyVehicle* 车辆, S_VehicleCommand& command) {
	auto* vehicle = dynamic_cast<S_PxVehicle*>(车辆);

	vehicle->m_Vehicle.mCommandState.brakes[0] = command.m_刹车;
	vehicle->m_Vehicle.mCommandState.brakes[1] = command.m_手刹;
	vehicle->m_Vehicle.mCommandState.nbBrakes = 2;
	vehicle->m_Vehicle.mCommandState.throttle = command.m_油门;
	vehicle->m_Vehicle.mCommandState.steer = command.m_转向;

	vehicle->m_Vehicle.mTransmissionCommandState.targetGear = command.m_挡位;
}

void f_phy_车轮动力总成(S_PhyVehicle* 车辆, const S_VehiclePowertrain& 动力总成) {
	auto* vehicle = dynamic_cast<S_PxVehicle*>(车辆);

	vehicle->m_Vehicle.mEngineDriveParams.engineParams.moi = 动力总成.m_惯性力矩;
	vehicle->m_Vehicle.mEngineDriveParams.engineParams.peakTorque = 动力总成.m_最大扭矩;
	vehicle->m_Vehicle.mEngineDriveParams.engineParams.idleOmega = 动力总成.m_怠速转速;
	vehicle->m_Vehicle.mEngineDriveParams.engineParams.maxOmega = 动力总成.m_最大转速;
	vehicle->m_Vehicle.mEngineDriveParams.engineParams.dampingRateFullThrottle = 动力总成.m_转速阻尼;
	vehicle->m_Vehicle.mEngineDriveParams.engineParams.dampingRateZeroThrottleClutchEngaged = 动力总成.m_结合阻尼;
	vehicle->m_Vehicle.mEngineDriveParams.engineParams.dampingRateZeroThrottleClutchDisengaged = 动力总成.m_分离阻尼;
	
}







